package org.noote.libs.shaky.comm;

import org.noote.libs.bluetooth.SPPEvent_Client;

import android.util.Log;

//////////////////////////////////
///
/// Only for Shaky Arduino Sketch 0.9 !
///
/////////////////////////////////

public class BluetoothCommandEvents implements SPPEvent_Client,BluetoothCommandEvents_Interface {
	static final String TAG = "BluetoothCommandHandler";
	private boolean _isDebug = false;

	@Override
	public void onConnected() {
	}
	@Override
	public void onDisconnected() {
	}
	@Override
	public void onError(String sError) {
	}
	@Override
	public void onText(String sText) {
	}
	@Override
	public void onCmd(String sMessage)
	{
    	if(_isDebug)
    		Log.d(TAG, "Get cmd : "+sMessage);
		String [] sCmds = sMessage.split("\\:");
		if(sCmds.length==2)
		{
			String sCmd = sCmds[0].trim().toLowerCase();
			String sOps = sCmds[1].trim().toLowerCase();
        	if(sCmd.equals("error"))
        	{
        		if(sOps.length()>0)
    			onError(sOps);
        	}
        	else if(sCmd.equals("message"))
        	{
        		if(sOps.length()>0)
        			onText(sOps);
        	}
        	else if(sCmd.equals("version"))
        	{
        		String robotname = "?", robotversion="?";
				String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        				
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
        			if(name.equals("name"))
        			{
        				robotname = val;
        			}
        			else if(name.equals("ver"))
        			{
        				robotversion = val;
        			}
        		}
        		
        		onVersion(robotname, robotversion);
        	}
        	else if(sCmd.equals("ready"))
        	{
        		String sStatus = "?";
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        				
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
        			if(name.equals("status"))
        			{
        				sStatus = val;
        			}
        		}
        		
        		onReady(sStatus);
        	}
        	else if(sCmd.equals("tick"))
        	{
        		float fTimeSlice=0;
        		String [] sDatas = sOps.split("\\,");
        		for(String d : sDatas)
        		{
        			String [] sVals = d.split("\\=");
        			if(sVals.length != 2) continue;
        				
        			String name = sVals[0].trim();
        			String val = sVals[1].trim();
        			
        			if(name.equals("slicetime"))
        			{
        				fTimeSlice = Long.parseLong(val)/1000.0f;
        			}
        		}
        		onTick(fTimeSlice);
        	}
        	else
        		onCommand(sCmd, sOps);
		}
    }

	@Override
	public void onCommand(String sCommand, String sOperations) {
	}
	@Override
	public void onVersion(String robotname, String robotversion) {
	}
	@Override
	public void onReady(String sStatus) {
	}
	@Override
	public void onTick(float fTimeSlice) {
	}
}
